﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vnFingerPrint.DataAccess;
using vnFingerPrint.BusinessObject;
namespace vnFingerPrint.GUI
{
    public partial class frmNhapCongDan : Form
    {
        public string mode = "add";
        string soCMND;
        public frmNhapCongDan()
        {
            InitializeComponent();
        }
        public frmNhapCongDan(string _soCMND)
        {
            InitializeComponent();
            soCMND = _soCMND;
            congDanMng1.SetInfo(CongDan_DataAccess.GetObject(_soCMND));
            mode = "edit";
            congDanMng1.setMode(mode);
            vtUtTrai.SoCMND = vtUtPhai.SoCMND = vtNhanTrai.SoCMND = vtNhanPhai.SoCMND = vtGiuaTrai.SoCMND
                            = vtGiuaPhai.SoCMND = vtTroTrai.SoCMND = vtTroPhai.SoCMND = vtCaiTrai.SoCMND = vtCaiPhai.SoCMND
                            = _soCMND;
            btnAdd.Visible = false;
        }
        private void frmNhapCongDan_Load(object sender, EventArgs e)
        {
            
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            lblMsg.Text = "";
            CongDan_Object newObj = congDanMng1.GetInfo();
            if (newObj != null)
                try
                {
                    if (mode == "add")
                    {
                        CongDan_DataAccess.Insert(newObj);
                        mode = "edit";
                        congDanMng1.setMode(mode);
                        if (soCMND == "" || soCMND == null) btnAdd.Visible = true;
                        lblMsg.Text = "Dữ liệu đã được ghi nhận!";
                        vtUtTrai.SoCMND = vtUtPhai.SoCMND = vtNhanTrai.SoCMND = vtNhanPhai.SoCMND = vtGiuaTrai.SoCMND
                            = vtGiuaPhai.SoCMND = vtTroTrai.SoCMND = vtTroPhai.SoCMND = vtCaiTrai.SoCMND = vtCaiPhai.SoCMND
                            = newObj.SoCMND;
                        //pbVanTay.Enabled = true;
                    }
                    else
                    {
                        CongDan_DataAccess.SaveChanged(newObj);
                        mode = "edit";
                        //pbVanTay.Enabled = true;
                        congDanMng1.setMode(mode);
                       if(soCMND==""||soCMND==null) btnAdd.Visible = true;
                        lblMsg.Text = "Dữ liệu đã được ghi nhận!";
                    }
                    if (newObj.ImageUrl != "" && newObj.ImageUrl != null) congDanMng1.SaveImage();
                }
                catch(Exception ex)
                {
                    lblMsg.Text = "Có lỗi xảy ra trong quá trình ghi nhận!" + newObj.ImageUrl;
                    congDanMng1.SaveImage();
                }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            mode = "add";
            congDanMng1.setMode(mode);
            congDanMng1.ClearInfo();
            lblMsg.Text = "";
            btnAdd.Visible = false;
            vtUtTrai.SoCMND = vtUtPhai.SoCMND = vtNhanTrai.SoCMND = vtNhanPhai.SoCMND = vtGiuaTrai.SoCMND
                            = vtGiuaPhai.SoCMND = vtTroTrai.SoCMND = vtTroPhai.SoCMND = vtCaiTrai.SoCMND = vtCaiPhai.SoCMND
                            = "";
           // pbVanTay.Enabled = false;
        }

    }
}
